//
//  PhotoBrowserViewController.m
//
//  Created by mac on 2023/3/1.
//

#import "PhotoBrowserViewController.h"
#import "PhotoBorwserCell.h"
#import <Masonry/Masonry.h>
#import "NSDictionary+Accessors.h"

@interface PhotoBrowserViewController ()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>
@property (strong, nonatomic) UICollectionView *collectionView;
@property (strong, nonatomic) UILabel *numLabel;
@property (assign, nonatomic) BOOL isShowed;
@end

@implementation PhotoBrowserViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupView];
}

- (void)setupView {
    __weak typeof(self) weakSelf = self;
    _collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:[[UICollectionViewFlowLayout alloc] init]];
    [self.view addSubview:_collectionView];
    _collectionView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    [_collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.insets(UIEdgeInsetsMake(0, 0, 0, 0));
    }];
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    
    _numLabel = [[UILabel alloc] init];
    [self.view addSubview:_numLabel];
    _numLabel.font = [UIFont systemFontOfSize:17];
    _numLabel.textAlignment = NSTextAlignmentCenter;
    _numLabel.numberOfLines = 1;
    _numLabel.textColor = [UIColor whiteColor];
    [_numLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(weakSelf.view.mas_safeAreaLayoutGuideBottom).offset(0);
        make.centerX.equalTo(weakSelf.view.mas_centerX);
    }];
    [_collectionView registerClass:[PhotoBorwserCell class] forCellWithReuseIdentifier:@"PhotoBorwserCell"];
    _numLabel.text = [NSString stringWithFormat:@"1/%td",_photos.count];
}

- (void)viewDidLayoutSubviews{
    [super viewDidLayoutSubviews];
    if(_index >= _photos.count){
        return;
    }
    if(!_isShowed){
        _isShowed = YES;
        [_collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:_index inSection:0] atScrollPosition:UICollectionViewScrollPositionNone animated:NO];
    }
}

- (void)setPhotos:(NSArray *)photos {
    NSMutableArray *newPhotos = [NSMutableArray new];
    for (id model in photos) {
        NSString *url = nil;
        if([model isKindOfClass:[NSDictionary class]]){
            url = [((NSDictionary *)model) ac_stringForKey:_key];
        }
        if([model isKindOfClass:[NSString class]]){
            url = (NSString *)model;
        }
        if(!url.length){  continue; }
        [newPhotos addObject:url];
    }
    _photos = newPhotos;
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    return _photos.count;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    return CGSizeMake(self.view.frame.size.width, self.view.frame.size.height);
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    PhotoBorwserCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"PhotoBorwserCell" forIndexPath:indexPath];
    cell.url = _photos[indexPath.row];
    __weak typeof(self) weakSelf = self;
    cell.optionBlock = ^{
        [weakSelf dismissViewControllerAnimated:YES completion:nil];
    };
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView willDisplayCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath {
    _numLabel.text = [NSString stringWithFormat:@"%td/%td",indexPath.item+1,_photos.count];
}

@end
